
var MP3MusicLike = (function() {
	
	var DEBUG = false;
	var _li = null;
	var _LIKE = 0;
	var _TOKEN = "";
	var _SONGID = "";
	var visitorBox	= null;
	var _ITEMCOUNT = 10;
	var _PAGE = 1;
	var tabLikeInit = false;
	
	function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + '.' + '$2');
		}
		return x1 + x2;
	}
	
	function boxVisitors(obj) {		
		zm("#like-list-user").remove();
		zm("#like-more").unbind("click");
		var _html = '<div id="like-list-user" class="list_user">' +
		'<div id="like-friend-list" class="friend_list">';
		for(var i=0;i<obj.result.list.length;i++) {
			_html +=	'<div class="row ffriend" id="div_pipimalov">'+								
			'<span class="avatar fleft _friend" style="margin-right:10px;" rel="ZMEA_'+obj.result.list[i]+'?width=35&height=35&id=1"></span>'+
			'<a href="profile" class="friends"><span rel="ZMED_'+obj.result.list[i]+'?width=35&height=35&id=1&l=2"></span></a>'+
			'</div>';
		}
		_html +=	'</div>';
		if(obj.result.total > _ITEMCOUNT * _PAGE) {
			_html +='<div id="like-more" class="hBtnC"><a title="Xem thêm" class="viewmore">Xem thêm</a></div>'
		}	
		_html +='</div>';
					
		if(visitorBox==null) {
			setBox = new zmCore.Boxy({
				title: 'Danh sách người thích bài hát',
				content: _html,
				modal: true,
				okButton: 'Đóng'
			});
		} else {
			setBox.setContent(_html);
		}
		setBox.show();		
		
		if (typeof(ZME_widget_mix) == 'function') {
			ZME_widget_mix();
		} else {
			zmCore.addScript('http://widget.me.zing.vn/' + 'js/zingme_widget_ex_1.7.1.min.js', function () {
				ZME_widget_mix();
			});
		}
		
		zm("#like-more").click(function() {
			_PAGE++;
			zmCore.getJSON(
				MP3.LIKE_URL_VISITOR+"?uid="+MP3.ACCOUNT_ID+"&sid="+_SONGID+"&token="+_TOKEN+"&page="+_PAGE+"&count="+_ITEMCOUNT,
				function(moreObj) {
					var liFL = zm("#like-friend-list");
					var htFL = "";
					for(var i=0;i<moreObj.result.list.length;i++) {
						htFL 	+=	'<div class="row ffriend" id="div_pipimalov">'+								
						'<span class="avatar fleft _friend" style="margin-right:10px;" rel="ZMEA_'+moreObj.result.list[i]+'?width=35&height=35&id=1"></span>'+
						'<a href="profile" class="friends"><span rel="ZMED_'+moreObj.result.list[i]+'?width=35&height=35&id=1&l=2"></span></a>'+
						'</div>';
					}
					liFL.append(htFL);
					if (typeof(ZME_widget_mix) == 'function') {
						ZME_widget_mix();
					} else {
						zmCore.addScript('http://widget.me.zing.vn/' + 'js/zingme_widget_ex_1.7.1.min.js', function () {
							ZME_widget_mix();
						});
					}
					
					if(obj.result.total <= _ITEMCOUNT * _PAGE) {
						zm("#like-more").unbind("click");
						zm("#like-more").hide();
					}
				}
				);
			return false;
		});
	}
	function statsBox(num){
		zm("#_numberLike").html(addCommas(num)+' người thích');
		if(zm('#_numberLikeBox').length){
			if(num == 0) {
				if(!zm('#_numberLikeBox').hasClass('none')) zm('#_numberLikeBox').addClass('none');
			} 
			else zm('#_numberLikeBox').removeClass('none');
		}
	}
	function stats(cbFunc){
		_TOKEN = _li.attr("rel_t");
		_SONGID = _li.attr("rel_k");
		zmCore.getJSON(
			MP3.LIKE_URL_GET+'?uid='+MP3.ACCOUNT_ID+'&sid='+_SONGID+"&token="+_TOKEN,
			function(obj) {
				if(typeof obj === 'object') {
					_li.html((obj.like?"Bỏ thích":"Thích"));
					_LIKE = obj.total;
					statsBox(_LIKE);
					if(obj.like) _li.attr('like',1);
					else _li.attr('like',0);					
					if(cbFunc) cbFunc();
				}
			}
			);	
	}
	function getToken(cbFunc){
		zmCore.get(MP3.MP3_URL + 'ajax/like/token?id='+_li.attr('pid'),
		{
			'dataType': 'json'
		},
		function (data) {
			if (!data.error) {
				_li.attr("rel_t",data.token);
				_li.attr("rel_k",data.key);
				if(cbFunc) cbFunc();
			}
		});
		
	}
	function init(){
		_TOKEN = _li.attr("rel_t");
		_SONGID = _li.attr("rel_k");
		stats(function(){
			_li.click(function(){
				if(!tabLikeInit){
					MP3Playlist.songID =  _li.attr("pid");
					MP3Playlist.initTabLike();
					tabLikeInit = true;
				}
				if (!MP3.ACCOUNT_ID) {
					return Login.show(function(){MP3MusicLike.getToken(function(){MP3MusicLike.stats();});
					});
				} else {
					likeClick(_li);
				}
				return false;
			});
			zm("#_numberLike").click(function(){
				_PAGE = 1;
				zmCore.getJSON(
					MP3.LIKE_URL_VISITOR+"?uid="+MP3.ACCOUNT_ID+"&sid="+_SONGID+"&token="+_TOKEN+"&page="+_PAGE+"&count="+_ITEMCOUNT,
					function(obj) {
						if(obj.result.total>0) boxVisitors(obj);
					}
					);	
			});
		});
	}
	
	function likeClick(_e)
	{
		_TOKEN = _e.attr("rel_t");
		_SONGID = _e.attr("rel_k");
		var url_service = "";		
		if(_e.attr('like')!=1) {
			url_service = MP3.LIKE_URL_ACTIVE+"?uid="+MP3.ACCOUNT_ID+"&sid="+_SONGID+"&token="+_TOKEN;
		} else {
			url_service = MP3.LIKE_URL_DEACTIVE+"?uid="+MP3.ACCOUNT_ID+"&sid="+_SONGID+"&token="+_TOKEN;
		}

		zmCore.getJSON(
			url_service,
			function(like) {								
				if(typeof like === 'object' && !like.error) {
					if(_e.attr('like')!=1) {
						if(_e.attr('rel')){
							MP3Tab.btnId = _e.attr('id');
							MP3Tab.active();
						}
						_e.html("Bỏ thích");
						_e.attr('like',1);
						_LIKE++;
						//ZingMe
						zmCore.get(MP3.MP3_URL+'ajax/like?oid='+_SONGID+'&token=' + _TOKEN);
						_gaq.push(['mp3._trackPageview', '/like'+location.pathname]);
					} else {
						_e.html("Thích");
						_e.attr('like',0);
						_LIKE--;
					}
					statsBox(_LIKE);
				}
			}
			);
	}
	
	return {
		init: function() {
			_li = zm('#_btnLike');
			if(_li == undefined || _li == null) return;
			init();
		},
		stats:function(){
			stats();
		},
		getToken:function(cbFunc){
			getToken(cbFunc);
		}
	};
})();

zmCore.ready(function(){	
	MP3MusicLike.init();
});
